﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Yes.Core.Context;

namespace Yes.Core.Helper
{
    public class CookiesHelper
    {
        #region 获取Cookie

          /// <summary>
          /// 获得Cookie的子键值
          /// </summary>
          /// <param name="cookie"></param>
          /// <param name="key"></param>
          /// <returns></returns>
          public static string GetCookieValue(string key)
        {
            var request = YesHttpContext.Current.Request;
              if (request != null)
              {
                return request.Cookies[key];
            }
              return "";
          }
  
          #endregion
  
        #region 删除Cookie
  
  
          /// <summary>
          /// 删除Cookie
          /// </summary>
          /// <param name="key"></param>
          public static void RemoveCookie(string key)
          {
            var response = YesHttpContext.Current.Response;
            if (response != null)
            {
                response.Cookies.Delete(key);
            }
        }

        #endregion

        #region 设置/修改Cookie


        public static void SetCookie(string key, string value)
        {
            var response = YesHttpContext.Current.Response;
            if (response != null)
            {
                CookieOptions option = new CookieOptions();
                option.Expires = DateTime.Now.AddMinutes(20);
                response.Cookies.Append(key, value, option);
            }
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void SetCookie(string key, string value, DateTime? expires)
         {
            var response = YesHttpContext.Current.Response;
            if (response != null)
            {
                CookieOptions option = new CookieOptions();
                option.Expires = expires.Value;

                response.Cookies.Append(key, value, option);
            }
        }
 


         #endregion
 




    }

    public class HttpCookie
    {
        public string Key { get; set; }

        public CookieOptions Option { get; set; }

        public string Value { get; set; }
    }
}
